package com.dream.service2.java8;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class Function1 {
    public static <T, R> Map<T, R> listToMap(List<T> list, Function<T, R> function) {
        HashMap<T, R> hashMap = new HashMap<>();
        for (T t : list) {
            hashMap.put(t, function.apply(t));
        }
        return hashMap;
    }


    public static void main(String[] args) {
        //输入字符串T,返回字符串大写形式R  Function<T,R>
//        Function<String,String> toUpperCase = str->str.toUpperCase();
//        String result = toUpperCase.apply("cccccccccc");
//        System.out.println(result);
//
//        //输入字符串T返回字符串的长度R Integer
//        Function<String,Integer> lengthFunction = str->str.length();
//        Integer length = lengthFunction.apply("sadasdad");
//        System.out.println(length);
//        Function<String,String> toUpperCase = str->str.toUpperCase();

//        //输入一个字符串.获取字符串的长度然后乘以2
//        Function<String,Integer> lengthFunction = str->str.length();
//        Function<Integer,Integer> doubleFunction = length->length*2;
//        Integer doubleLength = lengthFunction.andThen(doubleFunction).apply("222asdasd");
//        System.out.println(doubleLength);

        //输入一个字符串List集合T返回一个Key为字符串本身,Value为字符串长度的Map
//        List<String> list = Arrays.asList("java", "js", "html");
//        //lambda方式
//        Function<String, Integer> lengthFunction = str -> str.length();
//        Map<String, Integer> listToMap = listToMap(list, lengthFunction);
//        System.out.println(listToMap);
//
//        //方法引用方式
//        Map<String,Integer> listToMap2 = listToMap(list,String::length);
//        System.out.println(listToMap2);

    }
}
